1006D - Two Strings Swaps - CodeForces Solution


implementation *1700

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;

int main() {
    cout.sync_with_stdio(0);
    cin.tie(0);

    int n; cin >> n;
    string a,b; cin >> a >> b;
    int res = 0;
    for (int i = 0, j = n-1; i <= j; i++, j--) {
        if (i == j) {
            if (a[i] != b[i]) res += 1;
            continue;
        }
        int cnt[26], ai = a[i]-'a', aj = a[j]-'a', bi = b[i]-'a', bj = b[j]-'a';
        cnt[ai] = 0, cnt[bi] = 0, cnt[aj] = 0, cnt[bj] = 0;
        cnt[ai]++, cnt[bi]++, cnt[aj]++, cnt[bj]++;
        if (cnt[bi] == 1 && cnt[bj] == 1) res+=2;
        else if (cnt[bi] > 1 && cnt[bj] > 1) {
            if (cnt[ai] > 1 && cnt[aj] > 1) continue;
            else res += 1;
        }
        else res += 1;
    }
    cout << res << endl;

    return 0;
}


Comments

Submit
0 Comments
More Questions

1143B - Nirvana
1285A - Mezo Playing Zoma
919B - Perfect Number
894A - QAQ
1551A - Polycarp and Coins
313A - Ilya and Bank Account
1469A - Regular Bracket Sequence
919C - Seat Arrangements
1634A - Reverse and Concatenate
1619C - Wrong Addition
1437A - Marketing Scheme
1473B - String LCM
1374A - Required Remainder
1265E - Beautiful Mirrors
1296A - Array with Odd Sum
1385A - Three Pairwise Maximums
911A - Nearest Minimums
102B - Sum of Digits
707A - Brain's Photos
1331B - Limericks
305B - Continued Fractions
1165B - Polycarp Training
1646C - Factorials and Powers of Two
596A - Wilbur and Swimming Pool
1462B - Last Year's Substring
1608B - Build the Permutation
1505A - Is it rated - 2
169A - Chores
765A - Neverending competitions
1303A - Erasing Zeroes